// 异步任务的并行流程控制 原理实现

const tasks = [task_1, task_2, task_3, task_4] 
let completedTasks = 0

function task_1() {
    setTimeout(() => {
        console.log('task 1');
        checkIfCompleted()
    }, 3000)
}

function task_2() {
    setTimeout(() => {
        console.log('task 2');
        checkIfCompleted()
    }, 1000)
}

function task_3() {
    setTimeout(() => {
        console.log('task 3');
        checkIfCompleted()
    }, 4000)
}

function task_4() {
    setTimeout(() => {
        console.log('task 4');
        checkIfCompleted()
    }, 100)
}

function checkIfCompleted() {
    completedTasks++
    if (completedTasks === tasks.length) {
        console.log('并行任务执行完成！')
    }
}

for (let task of tasks) {
    task()
}